package com.todo.user.application.convert;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.utils.DateUtils;
import com.todo.user.application.entity.bo.UserAccountBO;
import com.todo.user.application.entity.bo.UserAccountConditionBO;
import com.todo.user.application.entity.bo.UserInfoDetailBO;
import com.todo.user.iface.backend.request.QueryUserAccountByConditionReq;
import com.todo.user.iface.backend.response.QueryUserAccountByConditionResp;
import com.todo.user.iface.backend.response.UserAccountDetailResp;
import org.apache.commons.collections4.CollectionUtils;
import com.google.common.collect.Lists;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/4/10 11:11
 * @description
 */
public class UserBackendConvert {

    public static UserAccountDetailResp convertToUserAccountDetailResp(UserInfoDetailBO userInfoDetailBO) {
        if (userInfoDetailBO == null) {
            return null;
        }
        return UserAccountDetailResp.builder()
                .id(userInfoDetailBO.getUserId())
                .nickName(userInfoDetailBO.getNickName())
                .headImg(userInfoDetailBO.getHeadImg())
                .signature(userInfoDetailBO.getSignature())
                .birthday(userInfoDetailBO.getBirthday())
                .gender(userInfoDetailBO.getGender() == null ? null :userInfoDetailBO.getGender().getType())
                .nativePlace(userInfoDetailBO.getNativePlace())
                .location(userInfoDetailBO.getLocation())
                .status(userInfoDetailBO.getUserStatus() == null ? null :userInfoDetailBO.getUserStatus().getType())
                .source(userInfoDetailBO.getSource() == null ? null :userInfoDetailBO.getSource().getType())
                .userType(userInfoDetailBO.getUserType())
                .createTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, userInfoDetailBO.getCreateTime()))
                .updateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, userInfoDetailBO.getUpdateTime()))
                .backgroundImg(userInfoDetailBO.getBackgroundImg())
                .interestList(userInfoDetailBO.getInterestList())
                .mbit(userInfoDetailBO.getMbit())
                .holidayType(userInfoDetailBO.getHolidayType() == null ? null : userInfoDetailBO.getHolidayType().getType())
                .travelPlanType(userInfoDetailBO.getTravelPlanType() == null ? null : userInfoDetailBO.getTravelPlanType().getType())
                .travelObjective(userInfoDetailBO.getTravelObjective())
                .holidayTypeDesc(userInfoDetailBO.getHolidayType() == null ? null : userInfoDetailBO.getHolidayType().getName())
                .travelPlanTypeDesc(userInfoDetailBO.getTravelPlanType() == null ? null : userInfoDetailBO.getTravelPlanType().getName())
                .promotion(userInfoDetailBO.getPromotion())
                .career(userInfoDetailBO.getCareer())
                .build();
    }

    public static PageResult<QueryUserAccountByConditionResp> convertToPageQueryUserAccount(PageResult<UserAccountBO> pageResult) {
        if (pageResult == null || CollectionUtils.isEmpty(pageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        PageResult<QueryUserAccountByConditionResp> result = new PageResult<>();
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setPageCount(pageResult.getPageCount());
        result.setPageSize(pageResult.getPageSize());
        result.setTotal(pageResult.getTotal());
        result.setPageNum(pageResult.getPageNum());
        result.setResults(pageResult.getResults().stream().map(UserBackendConvert::convertToQueryUserAccountByConditionResp).collect(Collectors.toList()));
        return result;
    }

    public static QueryUserAccountByConditionResp convertToQueryUserAccountByConditionResp(UserAccountBO userAccountBO) {
        if (userAccountBO == null) {
            return null;
        }
        return QueryUserAccountByConditionResp.builder()
                .id(userAccountBO.getId())
                .userName(userAccountBO.getUserName())
                .mobile(userAccountBO.getMobile())
                .nickName(userAccountBO.getNickName())
                .birthday(userAccountBO.getBirthday())
                .gender(userAccountBO.getGender())
                .nativePlace(userAccountBO.getNativePlace())
                .isReal(userAccountBO.getIsReal())
                .headImg(userAccountBO.getHeadImg())
                .location(userAccountBO.getLocation())
                .status(userAccountBO.getStatus().getType())
                .userType(userAccountBO.getUserType())
                .registerDay(DateUtils.differentDaysByMillisecond(userAccountBO.getCreateTime(), new Date()))
                .build();
    }

    public static UserAccountConditionBO buildUserAccountConditionBO(QueryUserAccountByConditionReq req) {
        if (req == null) {
            return null;
        }
        UserAccountConditionBO userAccountConditionBO = new UserAccountConditionBO();
        userAccountConditionBO.setUserId(req.getUserId());
        userAccountConditionBO.setUserName(req.getUserName());
        userAccountConditionBO.setMobile(req.getMobile());
        userAccountConditionBO.setLocation(req.getLocation());
        userAccountConditionBO.setStatus(req.getStatus());
        userAccountConditionBO.setNickName(req.getNickName());
        userAccountConditionBO.setPageNum(req.getPageIndex());
        userAccountConditionBO.setPageSize(req.getPageSize());

        return userAccountConditionBO;
    }
}
